package com.wutuo.aggragation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;

import cn.apestar.agg.common.security.LoginRequireInterceptor;
import cn.apestar.core.framework.DefaultSiteWebConfig;
import cn.apestar.core.framework.request.RequestContext;
import cn.apestar.core.framework.request.RequestContextInterceptor;

/**
 * 配置文件
 * @author Kent
 */
@Configuration
public class WebConfig extends DefaultSiteWebConfig {

    @Autowired
    Environment environment;

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("UTF-8");
        multipartResolver.setMaxInMemorySize(4096);
        // multipartResolver.setMaxUploadSize(1048576 * 2); // 最大尺寸为2M
        return multipartResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/libs/**").addResourceLocations("/libs/");
        registry.addResourceHandler("/module/**").addResourceLocations("/module/");

        // registry.addResourceHandler(siteSettings().getUploadFilePath() + "/**")
        // .addResourceLocations(siteSettings().getUploadFilePath() + "/");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Bean
    public DefaultServletHttpRequestHandler defaultServletHttpRequestHandler() {
        DefaultServletHttpRequestHandler delDefaultServletHttpRequestHandler = new DefaultServletHttpRequestHandler();
        return delDefaultServletHttpRequestHandler;
    }

    @Override
    public void addCustomInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginRequireInterceptor());
    }

    @Bean
    @Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public RequestContext requestContext() {
        return new RequestContext();
    }

    @Bean
    public LoginRequireInterceptor loginRequireInterceptor() {
        return new LoginRequireInterceptor();
    }

    @Bean
    public RequestContextInterceptor requestContextInterceptor() {
        return new RequestContextInterceptor();
    }

}
